package usf.gwt.ui.bootstrap.client;

import usf.gwt.ui.bootstrap.client.core.Constants;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.HasText;

public class TextBox extends FormControl<String> implements HasText {
	
	public TextBox() {
		super();
	}
    @Override
    protected Element initWidget() {
    	return Document.get().createTextInputElement();
    }
    @Override
    protected void initStyle() {
    	getBaseElement().setClassName(DEFAULT_FORM_CONTROL_STYLE);
    }

    public void setPlaceholder(String placeholder) {
    	getBaseElement().setAttribute(Constants.ATTRIB_PLACEHOLDER, placeholder);
    }
    public String getPlaceholder() {
        return getBaseElement().getAttribute(Constants.ATTRIB_PLACEHOLDER);
    }
    public void setAutocomplete(boolean autocomplete) {
    	getBaseElement().setAttribute(Constants.ATTRIB_AUTOCOMPLETE, autocomplete ? Constants.VALUE_ON : Constants.VALUE_OFF);
    }
    public boolean getAutocomplete() {
        return Constants.VALUE_ON.equals(getBaseElement().getAttribute(Constants.ATTRIB_AUTOCOMPLETE));
    }
    public void setMaxLength(int max) {
    	getBaseElement().setMaxLength(max);
	}
    public int getMaxLength() {
    	return getBaseElement().getMaxLength();
	}

    @Override
    public String getText() {
        return getText(getBaseElement());
    }
    @Override
    public void setText(String text) {
        setText(getBaseElement(), text);
    }
    
    @Override
    protected String getChangedValue(NativeEvent e) {
    	return getText();
    }

    protected final static native String getText(final Element e) /*-{
		return $wnd.$(e).val();
    }-*/;
    protected final static native void setText(final Element e, String text) /*-{
		$wnd.$(e).val(text);
    }-*/;

    
}
